package com.bestcem.xm.award.service.impl;

import cn.hutool.core.lang.Validator;
import com.bestcem.xm.award.constant.Constants;
import com.bestcem.xm.award.constant.RequestAwardConstants;
import com.bestcem.xm.award.entity.mongo.BaseEntity;
import com.bestcem.xm.award.entity.pojo.AwardAwardDO;
import com.bestcem.xm.award.entity.pojo.AwardRequestDO;
import com.bestcem.xm.award.enums.AwardRequestStatusEnum;
import com.bestcem.xm.award.enums.AwardTypeEnum;
import com.bestcem.xm.award.service.bo.award.AwardAwardBO;
import com.bestcem.xm.award.service.dto.requestaward.AliPayAwardDTO;
import com.bestcem.xm.award.service.dto.requestaward.RequestAwardDTO;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.uitls.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Optional;

import static com.bestcem.xm.award.enums.AwardRequestStatusEnum.PASS;
import static com.bestcem.xm.award.enums.AwardTypeEnum.ALIPAY;

/**
 * 阿里优惠券奖励
 *
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/2 14:22
 */
@Slf4j
@Service
public class AliPayAwardServiceImpl extends AbstractAwardAwardService {


    @Override
    public AwardTypeEnum getAwardTypeEnum() {
        return AwardTypeEnum.ALIPAY;
    }

    @Override
    public ServiceResult<String> doRequestAward(RequestAwardDTO requestAwardDTO) {
        // 返回结构体
        AliPayAwardDTO aliPayAwardDTO = new AliPayAwardDTO();
        aliPayAwardDTO.setTType(ALIPAY.getIndex());
        // 时间校验
        AwardRequestStatusEnum checkCostTime = awardCheck.getCheckCostTimeResult(requestAwardDTO);
        if (checkCostTime.getIndex() != PASS.getIndex()) {
            return super.checkFail(requestAwardDTO.getAwardRequestDO(), checkCostTime);
        }
        // 奖励id
        String awardId = Optional.ofNullable(requestAwardDTO).map(RequestAwardDTO::getAwardAwardDO)
                .map(BaseEntity::getId).orElse(Constants.BLANK);
        if (StringUtils.isBlank(awardId)) {
            return ServiceResult.fail(RequestAwardConstants.MISSING_AWARD_ID);
        }
        // 设备信息
        int tty = Optional.ofNullable(requestAwardDTO).map(RequestAwardDTO::getTty).orElse(Constants.MINUS_ONE);
        if (tty < Constants.ZERO) {
            return ServiceResult.fail(RequestAwardConstants.DEVICE_TYPE_ERROR);
        }
        // 奖励信息 返回页面配置 page.pc或page.mobile
        AwardAwardDO awardAwardDO = requestAwardDTO.getAwardAwardDO();
        // 页面配置
        AwardAwardDO.PageFront page = tty == 0
                ? Optional.ofNullable(awardAwardDO).map(AwardAwardDO::getPage).map(AwardAwardDO.Page::getPc).orElse(null)
                : Optional.ofNullable(awardAwardDO).map(AwardAwardDO::getPage).map(AwardAwardDO.Page::getMobile).orElse(null);
        aliPayAwardDTO.setPage(page);
        // 待插入AWARD_REQUEST数据
        AwardRequestDO request = Optional.of(requestAwardDTO).map(RequestAwardDTO::getAwardRequestDO).orElse(null);
        if (Validator.isNull(request)) {
            return ServiceResult.fail(RequestAwardConstants.MISSING_AWARD_REQUEST);
        }
        request.setStatus(PASS.getIndex());
        // 插入AWARD_REQUEST数据
        awardRequestDao.insert(request);
        String data = JsonUtil.transferToJsonSnake(aliPayAwardDTO);
        return ServiceResult.success(data);
    }

    @Override
    public ServiceResult<String> independentCheckAward(AwardAwardBO awardAward) {
        return doCheckWechatPage(awardAward);
    }
}
